package com.atjinggu.jinggumail.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author jingming
 * @Type ListIntegerValueValidator.java
 * @Desc
 * @date 2020/12/19 17:06
 */
public class ListIntegerValueValidator implements ConstraintValidator<ListVal,Integer> {
    private Set<Integer> set = new HashSet();
    @Override
    public void initialize(ListVal constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        if(vals.length != 0){
            Arrays.stream(vals).forEach(item -> {
                set.add(item);
            });
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}